package com.sinosoft.perfect.system._authorization.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.sinosoft.perfect.system._.po.TUserRolebusinessRelation;
import com.sinosoft.perfect.system._.util.Producer;
import com.sinosoft.perfect.system._authorization.dao.IUserRolebusinessAuthorizeDao;
import com.sinosoft.perfect.system._user.service.IUserService;

public class UserRolebusinessAuthorizeImpl implements IUserRolebusinessAuthorizeService {

	private IUserRolebusinessAuthorizeDao iuserRolebusinessAuthorizeDao;

	private IUserService userService;

	public void setIuserRolebusinessAuthorizeDao(IUserRolebusinessAuthorizeDao iuserRolebusinessAuthorizeDao) {
		this.iuserRolebusinessAuthorizeDao = iuserRolebusinessAuthorizeDao;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	@Override
	public List<TUserRolebusinessRelation> queryRolebusinessOfUser(String userId) throws SQLException {
		return iuserRolebusinessAuthorizeDao.queryRolebusinessOfUser(userId);
	}

	@Override
	public boolean delAllRoleUsersOfUnitAndAddUsersRoleOfUnit(String unitId, String chooseRoleBusinessString) {
		boolean boolResult = true;
		String[] chooseRoleBusiness = this.getChooseRoleBusinessFromString(chooseRoleBusinessString);

		List<String> userIds = null;
		List<TUserRolebusinessRelation> tuserRBByUnitResultList = null;

		if (chooseRoleBusiness != null && chooseRoleBusiness.length > 0) {
			try {
				userIds = iuserRolebusinessAuthorizeDao.queryUserIdsOfUnit(unitId);

			} catch (SQLException e) {
				boolResult = false;
				e.printStackTrace();
			}
			if (userIds != null) {
				for (int i = 0; i < userIds.size(); i++) {
					String userId = userIds.get(i);
					List<TUserRolebusinessRelation> tuserRBRResultList = this.getURBsByUserAndchooseRoleBusiness(userId, chooseRoleBusiness);
					if (tuserRBRResultList != null) {
						if (tuserRBByUnitResultList == null)
							tuserRBByUnitResultList = new ArrayList<TUserRolebusinessRelation>();
						tuserRBByUnitResultList.addAll(tuserRBRResultList);
					}
				}
			}
		}
		try {
			if (tuserRBByUnitResultList != null) {
				for (int i = 0; i < tuserRBByUnitResultList.size(); i++) {
					TUserRolebusinessRelation userRoleBusinessRelation = tuserRBByUnitResultList.get(i);
					int sortNum = userService.selectTUserMAXSortNumFromRoleBusiness(userRoleBusinessRelation.getRoleBusinessId()) + i + 1;
					userRoleBusinessRelation.setUserSortNumber(new Long(sortNum));
				}
			}
			iuserRolebusinessAuthorizeDao.delAllRoleUsersOfUnitAndAddUsersRoleOfUnit(unitId, tuserRBByUnitResultList);
		} catch (SQLException e) {
			boolResult = false;
			e.printStackTrace();
		}
		return boolResult;
	}

	@Override
	public boolean delAllRolesOfUserAndAddUserRoles(String userId, String chooseRoleBusinessString) {
		boolean boolResult = true;
		String[] chooseRoleBusiness = this.getChooseRoleBusinessFromString(chooseRoleBusinessString);

		List<TUserRolebusinessRelation> tuserRBResultList = null;
		if (chooseRoleBusiness != null && chooseRoleBusiness.length > 0) {
			tuserRBResultList = this.getURBsByUserAndchooseRoleBusiness(userId, chooseRoleBusiness);
		}
		try {
			if (tuserRBResultList != null) {
				for (int i = 0; i < tuserRBResultList.size(); i++) {
					TUserRolebusinessRelation userRoleBusinessRelation = tuserRBResultList.get(i);
					int sortNum = userService.selectTUserMAXSortNumFromRoleBusiness(userRoleBusinessRelation.getRoleBusinessId()) + i + 1;
					userRoleBusinessRelation.setUserSortNumber(new Long(sortNum));
				}
			}
			iuserRolebusinessAuthorizeDao.delAllRolesOfUserAndAddUserRoles(userId, tuserRBResultList);
		} catch (SQLException e) {
			boolResult = false;
			e.printStackTrace();
		}
		return boolResult;
	}

	private String[] getChooseRoleBusinessFromString(String chooseRoleBusinessString) {
		if (chooseRoleBusinessString != null) {
			return chooseRoleBusinessString.split(",");
		}
		return null;
	}

	private List<TUserRolebusinessRelation> getURBsByUserAndchooseRoleBusiness(String userId, String[] chooseRoleBusiness) {
		List<TUserRolebusinessRelation> tuserRBResultList = null;
		for (int i = 0; i < chooseRoleBusiness.length; i++) {
			if (tuserRBResultList == null)
				tuserRBResultList = new ArrayList<TUserRolebusinessRelation>();
			String chooseRoleBusinessString = chooseRoleBusiness[i];
			TUserRolebusinessRelation tuserRolebusinessRelation = this.getNewTUserRolebusinessRelation(userId, chooseRoleBusinessString);
			tuserRBResultList.add(tuserRolebusinessRelation);
		}
		return tuserRBResultList;
	}

	private TUserRolebusinessRelation getNewTUserRolebusinessRelation(String userId, String roleBusinessId) {
		TUserRolebusinessRelation tuserRolesysRelation = new TUserRolebusinessRelation();
		String manageId = Producer.generateIdOfTen();
		tuserRolesysRelation.setManageId(manageId);
		tuserRolesysRelation.setRoleBusinessId(roleBusinessId);
		tuserRolesysRelation.setUserMainId(userId);
		return tuserRolesysRelation;
	}
	
	@Override
	public boolean deleteAndinsertAllUsersInOneRoleBusiness(String addUserIds, String delUserIds, String roleBusinessId) throws SQLException {
		boolean flag = false;
		if (delUserIds != null && !"".equals(delUserIds)) {
			String delUserIdArray[] = delUserIds.split(",");
			for (int i = 0; i < delUserIdArray.length; i++) {
				TUserRolebusinessRelation tUserRolebusinessRelation = new TUserRolebusinessRelation();
				tUserRolebusinessRelation.setRoleBusinessId(roleBusinessId);
				tUserRolebusinessRelation.setUserMainId(delUserIdArray[i]);
				iuserRolebusinessAuthorizeDao.deleteTUserRolebusinessRelation(tUserRolebusinessRelation);
				flag = true;
			}
		}
		if (addUserIds != null && !"".equals(addUserIds)) {
			String addUserIdArray[] = addUserIds.split(",");
			for (int i = 0; i < addUserIdArray.length; i++) {
				TUserRolebusinessRelation userRoleBusinessRelation = new TUserRolebusinessRelation();
				userRoleBusinessRelation.setManageId(Producer.generateIdOfTen());
				userRoleBusinessRelation.setRoleBusinessId(roleBusinessId);
				userRoleBusinessRelation.setUserMainId(addUserIdArray[i]);
				userRoleBusinessRelation.setUserSortNumber(new Long(userService.selectTUserMAXSortNumFromRoleBusiness(userRoleBusinessRelation.getRoleBusinessId()) + i + 1));
				iuserRolebusinessAuthorizeDao.insertByTUserRolebusinessRelation(userRoleBusinessRelation);
				flag = true;
			}
		}
		return flag;
	}

}
